package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Api(tags = "回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Autowired
    private IArticleReplyService articleReplyService;
    @Autowired
    private IArticleService articleService;

    @ApiOperation("新增回复")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("回复的文章id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复内容") @RequestParam("content") @NonNull String content,
                            @ApiParam("一楼回复id") @RequestParam(value = "replyId",required = false) Long replyId,
                            @ApiParam("回复一楼用户id") @RequestParam(value = "replyUserId",required = false) Long replyUserId) {
        //  1. 检验用户状态
        User user = (User) request.getSession(false).getAttribute(AppConfig.USER_SESSION_KEY);
        if(user.getState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_USER_BANNED.getMessage());
        }

        //  2. 封装对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        if(replyId != null) {
            articleReply.setReplyId(replyId);
        }
        if(replyUserId != null) {
            articleReply.setReplyUserId(replyUserId);
        }

        //  3. 调用mapper
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @ApiOperation("获取文章下的所有回复")
    @GetMapping("/getreplies/one")
    public AppResult getRepliesByArticleId(@ApiParam("文章id") @RequestParam("articleId") @NonNull Long articleId) {
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }

    @ApiOperation("获取二楼回复")
    @GetMapping("/getreplies/two")
    public AppResult getRepliesByReplyId(@ApiParam("文章id") @RequestParam("articleId") @NonNull Long articleId,
                                         @ApiParam("一楼回复id") @RequestParam("replyId") @NonNull Long replyId) {
        List<ArticleReply> articleReplies = articleReplyService.selectByReplyId(articleId, replyId);
        return AppResult.success(articleReplies);
    }
}
